﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using System.Data;
using CrystalDecisions.Shared;

namespace CR_With_MVC.AspNetForms
{
    public partial class aspnetgeneric : System.Web.UI.Page
    {

        /// <summary>
        /// This is generic report viewer
        /// This ReportViewer will load report with the report name Session["ReportName"]
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {
                bool isValid = true;

                string strReportName = System.Web.HttpContext.Current.Session["ReportName"].ToString();
                //string strProduct = System.Web.HttpContext.Current.Session["rptProduct"].ToString();
                string strMonthshow = System.Web.HttpContext.Current.Session["rptMonthShow"].ToString();
                string strFromDate = System.Web.HttpContext.Current.Session["rptFromDate"].ToString();
                // Setting ToDate
                string strToDate = System.Web.HttpContext.Current.Session["rptToDate"].ToString();
                // Setting Report Data Source     
                //var rptSource = System.Web.HttpContext.Current.Session["rptSource"];
                DataTable _dt = (DataTable)System.Web.HttpContext.Current.Session["rptSource"];
                if (string.IsNullOrEmpty(strReportName)) // Checking is Report name provided or not
                {
                    isValid = false;
                }


                if (isValid) // If Report Name provided then do other operation
                {

                    _dt.TableName = "dt_Complain";
                    DataSet ds = new DataSet();
                    ds.Merge(_dt);

                    ReportDocument rd = new ReportDocument();
                    string strRptPath = Server.MapPath("~/") + "Rpts//" + strReportName;
                    //Loading Report
                    rd.Load(strRptPath);

                    // Setting report data source
                    if (_dt != null && _dt.Rows.Count > 0)
                        rd.SetDataSource(ds);

                    //if (!string.IsNullOrEmpty(strProduct))
                    //    rd.SetParameterValue("product", strProduct);

                    if (!string.IsNullOrEmpty(strMonthshow))
                        rd.SetParameterValue("monthshow", strMonthshow);

                    if (!string.IsNullOrEmpty(strFromDate))
                        rd.SetParameterValue("fromDate", strFromDate);
                    if (!string.IsNullOrEmpty(strToDate))
                        rd.SetParameterValue("toDate", strToDate);
                    CrystalReportViewer1.ReportSource = rd;

                    rd.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport");

                    ConnectionInfo myConnectionInfo = new ConnectionInfo();
                    myConnectionInfo.ServerName = "61.19.248.21";
                    myConnectionInfo.DatabaseName = "DVS";
                    myConnectionInfo.UserID = "dvsuser";
                    myConnectionInfo.Password = "adminsql";
                    setDBLOGONforREPORT(myConnectionInfo);


                    Session["ReportName"] = "";
                    //Session["product"] = "";
                    Session["monthshow"] = "";
                    Session["rptFromDate"] = "";
                    Session["rptToDate"] = "";
                    Session["rptSource"] = "";
                }
                else
                {
                    Response.Write("<H2>Nothing Found; No Report name found</H2>");
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
        private void setDBLOGONforREPORT(ConnectionInfo myconnectioninfo)
        {
            TableLogOnInfos mytableloginfos = new TableLogOnInfos();
            mytableloginfos = CrystalReportViewer1.LogOnInfo;
            foreach (TableLogOnInfo myTableLogOnInfo in mytableloginfos)
            {
                myTableLogOnInfo.ConnectionInfo = myconnectioninfo;
            }

        }
    }
}
